package bp.sys;

import bp.da.*;
import bp.en.*;
import bp.en.Map;
/** 
 数据版本
*/
public class FrmDBVer extends EntityMyPK
{

		///#region 基本属性
	/** 
	 权限控制
	*/
	@Override
	public UAC getHisUAC()
	{
		UAC uac = new UAC();
		uac.Readonly();
		uac.IsView = false;
		return uac;
	}
	public final String getTrackID()  {
		return this.GetValStringByKey(FrmDBVerAttr.TrackID);
	}
	public final void setTrackID(String value){
		this.SetValByKey(FrmDBVerAttr.TrackID, value);
	}

	public final String getTrackIDs()  {
		return this.GetValStringByKey(FrmDBVerAttr.TrackIDs);
	}
	public final void setTrackIDs(String value){
		this.SetValByKey(FrmDBVerAttr.TrackIDs, value);
	}

	public final String getKeyOfEn()  {
		return this.GetValStringByKey(FrmDBVerAttr.KeyOfEn);
	}
	public final void setKeyOfEn(String value){
		this.SetValByKey(FrmDBVerAttr.KeyOfEn, value);
	}
	public final String getFrmID()  {
		return this.GetValStringByKey(FrmDBVerAttr.FrmID);
	}
	public final void setFrmID(String value){
		this.SetValByKey(FrmDBVerAttr.FrmID, value);
	}

	public final String getFrmName()  {
		return this.GetValStringByKey(FrmDBVerAttr.FrmName);
	}
	public final void setFrmName(String value){
		this.SetValByKey(FrmDBVerAttr.FrmName, value);
	}
	/** 
	 主键值键
	*/
	public final String getRefPKVal()  {
		return this.GetValStringByKey(FrmDBVerAttr.RefPKVal);
	}
	public final void setRefPKVal(String value){
		this.SetValByKey(FrmDBVerAttr.RefPKVal, value);
	}
	/** 
	 FK_Emp
	*/
	public final String getRecNo()  {
		return this.GetValStringByKey(FrmDBVerAttr.RecNo);
	}
	public final void setRecNo(String value){
		this.SetValByKey(FrmDBVerAttr.RecNo, value);
	}
	public final String getRecName()  {
		return this.GetValStringByKey(FrmDBVerAttr.RecName);
	}
	public final void setRecName(String value){
		this.SetValByKey(FrmDBVerAttr.RecName, value);
	}
	public final String getRDT()  {
		return this.GetValStringByKey(FrmDBVerAttr.RDT);
	}
	public final void setRDT(String value){
		this.SetValByKey(FrmDBVerAttr.RDT, value);
	}
	public final int getVer()  {
		return this.GetValIntByKey(FrmDBVerAttr.FrmVer);
	}
	public final void setVer(int value){
		this.SetValByKey(FrmDBVerAttr.FrmVer, value);
	}

		///#endregion


		///#region 构造方法
	/** 
	 数据版本
	*/
	public FrmDBVer()
	{
	}
	public FrmDBVer(String mypk) throws Exception {
		this.setMyPK(mypk);
		this.Retrieve();
	}
	/** 
	 EnMap
	*/
	@Override
	public Map getEnMap()
	{
		if (this.get_enMap() != null)
		{
			return this.get_enMap();
		}
		Map map = new Map("Sys_FrmDBVer", "数据版本");
		map.AddMyPK();

		map.AddTBString(FrmDBVerAttr.FrmID, null, "表单ID", true, true, 0, 100, 20);
		map.AddTBString(FrmDBVerAttr.FrmName, null, "表单名称", true, true, 0, 100, 20); //@hongyan
		map.AddTBInt(FrmDBVerAttr.NodeID, 0, "节点编号", true, true);
		map.AddTBString(FrmDBVerAttr.NodeName, null, "节点名称", true, true, 0, 50, 20);

		map.AddTBInt(FrmDBVerAttr.FrmVer, 0, "表单版本号", true, true); //@hongyan
		map.AddTBString(FrmDBVerAttr.RefPKVal, null, "表单主键值", true, true, 0, 40, 20);

		map.AddTBString(FrmDBVerAttr.ChangeFields, null, "修改的字段", true, true, 0, 3900, 20);
		map.AddTBInt(FrmDBVerAttr.ChangeNum, 0, "修改的字段数量", true, true);

		map.AddTBString(FrmDBVerAttr.TrackID, null, "TrackID", true, true, 0, 40, 20);

		map.AddTBString(FrmDBVerAttr.RecNo, null, "记录人", true, true, 0, 30, 20);
		map.AddTBString(FrmDBVerAttr.RecName, null, "用户名", true, true, 0, 30, 20);
		map.AddTBDateTime(FrmDBVerAttr.RDT, null, "记录时间", true, true);

		map.AddTBString(FrmDBVerAttr.KeyOfEn, null, "章节字段有效", true, true, 0, 100, 20);
		map.AddTBString(FrmDBVerAttr.TrackIDs, null, "TrackIDs", true, true, 0, 500, 20);
		map.AddBoolean(FrmDBVerAttr.IsChange, false, "是否改变", true, false);
		this.set_enMap(map);
		return this.get_enMap();
	}

		///#endregion


		///#region 增加版本.


	/** 
	 增加主表、从表的数据
	 
	 @param frmID 表单ID
	 @param refPKVal 关联值
	 @param trackID
	 @param jsonOfFrmDB
	 @param frmDtlDB
	 @param frmAthDB
	*/

	public static void AddFrmDBTrack(int ver, String frmID, String refPKVal, String trackID, String jsonOfFrmDB, String frmDtlDB, String frmAthDB, int nodeID, String nodeName) throws Exception {
		AddFrmDBTrack(ver, frmID, refPKVal, trackID, jsonOfFrmDB, frmDtlDB, frmAthDB, nodeID,nodeName,false);
	}

	public static void AddFrmDBTrack(int ver, String frmID, String refPKVal, String trackID, String jsonOfFrmDB, String frmDtlDB, String frmAthDB, int nodeID, String nodeName, boolean isChartFrm) throws Exception {
		if (jsonOfFrmDB == null && isChartFrm == false)
			return;
		boolean isChange = false;
		if (trackID.equals("CurrVer_" + refPKVal))
			isChange = true;
		if(isChange == false)
		{
			//trackID不是CurrVer_时，删除保存的数据
			if(trackID.startsWith("CurrVer_") == false)
			{
				//删除数据
				FrmDBVers dbVers = new FrmDBVers();
				dbVers.Delete(FrmDBVerAttr.FrmID, frmID, FrmDBVerAttr.RefPKVal, refPKVal, FrmDBVerAttr.TrackID, "CurrVer_" + refPKVal);
			}
			FrmDBVer frmDBVer = GetLastFrmDBVer(ver, frmID, refPKVal,"");
			if (frmDBVer == null)
				isChange = true;
			if (frmDBVer != null)
			{
				//获取历史的主表信息
				String mainJson = DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", frmDBVer.getMyPK(), "FrmDB");
				if (mainJson.equals(jsonOfFrmDB) == false)
					isChange = true;
				if (isChange == false)
				{
					String dtlJson = DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", frmDBVer.getMyPK(), "FrmDtlDB");
					if (dtlJson.equals(frmDtlDB) == false)
						isChange = true;
				}
				if (isChange == false)
				{
					String athJson = DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", frmDBVer.getMyPK(), "FrmAthDB");
					if (athJson.equals(frmAthDB) == false)
						isChange = true;
				}
			}
		}

		FrmDBVer en = new FrmDBVer();
		en.setFrmID(frmID);
		MapData md = new MapData(frmID);
		en.setFrmName(md.getName());
		en.setRefPKVal(refPKVal);
		en.setTrackID(trackID);
		en.SetValByKey(FrmDBVerAttr.IsChange, isChange?1:0);
		en.setVer(ver);
		en.SetValByKey(FrmDBVerAttr.NodeID, nodeID);
		en.SetValByKey(FrmDBVerAttr.NodeName, nodeName);
		en.Insert();
		if(md.getHisFrmType() == FrmType.VSTOForExcel || md.getHisFrmType() == FrmType.VSTOForWord)
		{
			//保存文件
			byte[] bty =  DBAccess.GetByteFromDB(md.getPTable(), "OID", refPKVal, MapDataAttr.VSTOExcelFile);
			DBAccess.SaveBytesToDB(bty, "Sys_FrmDBVer", "MyPK", en.getMyPK(), MapDataAttr.VSTOExcelFile);
		}
		//保存主表数据.
		if (DataType.IsNullOrEmpty(jsonOfFrmDB) == false)
		{
			DBAccess.SaveBigTextToDB(jsonOfFrmDB, "Sys_FrmDBVer", "MyPK", en.getMyPK(), "FrmDB");
		}

		//保存从表数据
		if (DataType.IsNullOrEmpty(frmDtlDB) == false)
		{
			DBAccess.SaveBigTextToDB(frmDtlDB, "Sys_FrmDBVer", "MyPK", en.getMyPK(), "FrmDtlDB");
		}

		//保存附件数据
		if (DataType.IsNullOrEmpty(frmAthDB) == false)
		{
			DBAccess.SaveBigTextToDB(frmAthDB, "Sys_FrmDBVer", "MyPK", en.getMyPK(), "FrmAthDB");
		}
	}
	/** 
	 保存章节表单的字段数据
	 
	 @param frmID
	 @param refPKVal
	 @param trackID
	 @param chartValue
	 @param keyOfEn
	*/
	public static void AddKeyOfEnDBTrack(int ver, String frmID, String refPKVal, String trackID, String chartValue, String keyOfEn, int nodeID, String nodeName) throws Exception {
		boolean isChange = false;
		FrmDBVer frmDBVer = GetLastFrmDBVer(ver,frmID,refPKVal, keyOfEn);
		if (frmDBVer == null)
			isChange = true;
		if (frmDBVer != null)
		{
			//获取历史的主表信息
			String mainJson = DBAccess.GetBigTextFromDB("Sys_FrmDBVer", "MyPK", frmDBVer.getMyPK(), "FrmDB");
			if (mainJson.equals(chartValue) == false)
				isChange = true;
		}
		FrmDBVer en = new FrmDBVer();
		en.setFrmID(frmID);
		en.setRefPKVal(refPKVal);
		en.setTrackID(trackID);
		en.setKeyOfEn(keyOfEn);
		en.setVer(ver);
		en.SetValByKey(FrmDBVerAttr.IsChange, isChange?1:0);
		en.SetValByKey(FrmDBVerAttr.NodeID, nodeID);
		en.SetValByKey(FrmDBVerAttr.NodeName, nodeName);
		en.Insert();

		//保存章节表单字段的数据.
		DBAccess.SaveBigTextToDB(chartValue, "Sys_FrmDBVer", "MyPK", en.getMyPK(), "FrmDB");
	}
	private static FrmDBVer GetLastFrmDBVer(int ver, String frmID,String workid,String keyOfEn) throws Exception {
		if (ver == 1)
			return null;

		FrmDBVers frmDBVers = new FrmDBVers();
		frmDBVers.Retrieve(FrmDBVerAttr.FrmID, frmID, FrmDBVerAttr.FrmVer, ver - 1, FrmDBVerAttr.RefPKVal, workid);
		if (frmDBVers.size() == 0) return null;
		for(FrmDBVer dbver : frmDBVers.ToJavaList())
		{
			if (DataType.IsNullOrEmpty(keyOfEn) && DataType.IsNullOrEmpty(dbver.getKeyOfEn()) == true)
				return dbver;
			if (DataType.IsNullOrEmpty(keyOfEn) == false && keyOfEn.equals(dbver.getKeyOfEn()) == true)
				return dbver;
		}
		return null;
	}
	@Override
	protected boolean beforeInsert() throws Exception
	{
		this.setMyPK(DBAccess.GenerGUID());
		this.SetValByKey(FrmDBVerAttr.RDT, DataType.getCurrentDateTime());

		if (DataType.IsNullOrEmpty(this.getRecNo()) == true)
		{
			this.setRecNo(bp.web.WebUser.getNo());
			this.setRecName(bp.web.WebUser.getName());
		}

		return super.beforeInsert();
	}

		///#endregion 增加版本.


		///#region 重写
	@Override
	public Entities GetNewEntities()
	{
		return new FrmDBVers();
	}

		///#endregion 重写
}
